<?php
/**
 * User: Allen
 * Date: 15-12-08
 * 反馈控制器
 */

class Opinion extends PC_Controller{
	
	protected $go_url = '/admin/opinion/opinion_list';	
	public function __construct(){
		parent::__construct();
		$this->load->model('opinion_model');
	}
	
	/**
	 * @brief 反馈信息页面
	 * @param Null
	 * @par 2015/12/08 Ver 1.00 Created by Allen
	 */	
	public function opinion_list() {
		$message = 1;
		if($this->uri->segment(4)) {
			$message = $this->uri->segment(4);
		}
		$this->data['message'] = $message;
		$this->data['username'] =  empty($_POST) ? '' : $_POST['username'];
		$this->data['ctime'] =  empty($_POST) ? '' : $_POST['ctime'];
		$this->data['etime'] =  empty($_POST) ? '' : $_POST['etime'];
		//判断权限
		$this->data['authority'] = $this->authority;
		$this->load->view('/admin/opinion_list.php',$this->data);
	}
	
	/**
	 * @brief ajxa获取文章分类信息
	 * @param Null
	 * @par 2015/12/08 Ver 1.00 Created by Allen
	 */
	public function ajax_opinion_list() {
		$data = $_GET;
		$start = $data['iDisplayStart']; //显示的起始索引
		$length = $data['iDisplayLength'];//显示的行数
		$sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列 
		$sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
		$search = $data ['sSearch'];//全局搜索字段
		$where['concat(id) like'] = '%'.trim($search).'%';
		$data['username'] ? $where['opinion.username like'] = '%'.trim($data['username']).'%' :'';
		if($data['ctime']) {
			$where['opinion.create_time >='] = strtotime($data['ctime']);
		}
		if($data['etime']) {
			$where['opinion.create_time <'] = strtotime($data['etime'])+86400;
		}
		$this->data['count'] = $this->opinion_model->getCount($where);
		$aaData = array();
		if($this->data['count']) {
			$order_by = $sort_th." ".$sort_type;
			$opinion_list = $this->opinion_model->getOpinionList($where,$length,$start,$order_by);
			$authority = $this->authority;
				$this->load->helper('string_helper');
			foreach($opinion_list as $key => $value) {
				if($value['content']) {
						$opinion_list[$key]['intro'] = cn_substr_utf8($value['content'],100)."...";
				}				
				if(!$value['litpic']) {
					$value['litpic'] = '/public_source/www/images/default.jpg';
				}
				$litpics = explode(',', $value['litpic']);
				$opinion_list[$key]['litpics'] = '';
				foreach($litpics as $k => $v) {
					$opinion_list[$key]['litpics'] .= '<img src="'.$v.'" style="max-height:30px;padding-left:20px;"/>';
				}								
				$opinion_list[$key]['create_time'] = date("Y-m-d H:i:s",$value['create_time']);
				$opinion_list[$key]['check'] = '<label><input name="c_id" type="checkbox" value="'.$value['id'].'" /></label>';								
				$del_url = '';
				if($authority['del_status'] == 2) {
					$del_url = '<a onclick="deleteOne(this)"  data_id = '.$value['id'].'><span class="btn btn-xs btn-pink">删除</span></a>';
				}
				$details_url = '';
				if($authority['details_status'] == 2) {
					$details_url = '<a href="/admin/opinion/look_details/'.$value['id'].'"><span class="btn btn-xs btn-primary">详情</span></a>';
				}

				$opinion_list[$key]['operate'] = $details_url." ".$del_url;
				if(empty($del_url) && empty($details_url)) {$opinion_list[$key]['operate'] = '无';};
			}
			$aaData = $opinion_list;
		}
		$output['aaData'] = $aaData;
		$output['sEcho'] = $_GET['sEcho'];
		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		echo json_encode($output); //最后把数据以json格式返回
	}
	
	/**
	 * @brief 删除信息
	 * @param Null
	 * @par 2015/12/08 Ver 1.00 Created by Allen
	 */
	public function delete() {
		$data = $_POST;
		$id = $_POST['id'];
		$where['id'] = $id;	
		$del_result = $this->opinion_model->deleteOpinion($where);
		echo json_encode($del_result);
	}	
	
	/**
	 * @brief 删除多行信息
	 * @param Null
	 * @par 2015/12/08 Ver 1.00 Created by Allen
	 */
	public function delete_all() {
		$data = $_POST;
		$id_list = array_filter(explode(',',$data['str']));
		//$id_list = array(3,4);
		$del_result = $this->opinion_model->deleteAll($id_list);
		echo json_encode($del_result);
	}
	
	/**
	 * @brief 查看详情信息
	 * @param Null
	 * @par 2015/12/08 Ver 1.00 Created by Allen
	 */
	public function look_details() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$opinion_info = $this->opinion_model->checkOpinion($where);
		if(!$opinion_info['litpic']) {
			$opinion_info['litpic'] = '/public_source/www/images/default.jpg';
		}
		$litpics = explode(',', $opinion_info['litpic']);
		$opinion_info['litpics'] = '';
		foreach($litpics as $k => $v) {
			$opinion_info['litpics'] .= '<img src="'.$v.'" style="max-height:200px;padding-right:20px;float:left;"/>';
		}
		$this->data['data'] = $opinion_info;
		$this->load->view('/admin/opinion_details.php',$this->data);
	}
	
}
?>